package com.pengweiwei.androidzygote.functions.base;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.annotations.SerializedName;
import com.pengweiwei.androidzygote.R;
import com.putong35.common.baseactfrag.LoadState;
import com.putong35.common.baseactfrag.adapter.RecyclerViewBaseAdapter;
import com.putong35.common.baseactfrag.fragment.ListFragment;
import com.putong35.common.baseactfrag.view.BottomLoadStateView;
import com.putong35.common.httprequest.Response;
import com.putong35.common.utils.io.AssetsUtil;

import java.util.List;

public class ListViewWithHeaderAndFooterFragment extends ListFragment {

    private final static String TEST_URL = "http://apitest.baoruan.com/launcher/default/index?version=9.150320&un=239BA6301606E596F70AC1E7B4808B23&name=recommendlist&channelId=ceshi_baoruanservice1";
    private int counter = 0;
    private static final String KEY_ID = "key_id";


    public ListViewWithHeaderAndFooterFragment() {

    }


    public static ListViewWithHeaderAndFooterFragment newInstance() {
        ListViewWithHeaderAndFooterFragment frag = new ListViewWithHeaderAndFooterFragment();
        Bundle args = new Bundle();
        frag.setArguments(args);
        return frag;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        startLoading();
        return view;
    }


    @Override
    public TemplateListAdapter createAdapter() {
        TemplateListAdapter adapter = new TemplateListAdapter(getActivity());
        return adapter;
    }


    @Override
    public void onStartLoading() {
        String responseStr = AssetsUtil.getStringFromAssets(getActivity(), "list_ui_test_data.txt");
        Response parser = new Response(responseStr);
        List<TemplateBean> list = parser.getDataListBean(TemplateBean.class);
        if (list != null && list.size() > 0) {
            getAdapter().getData().addAll(list);
        }

        counter++;
        if (counter == 2) {
            changeLoadState(LoadState.ERROR);

        } else if (counter == 3) {
            changeLoadState(LoadState.FINISH);

        } else {
            changeLoadState(LoadState.IDLE);
        }
    }


    @Override
    public BottomLoadStateView createBottomLoadStateView(LayoutInflater inflater) {
        // idle 状态的UI
        TextView idle = new TextView(getActivity());
        idle.setText("waiting...");

        // 某一页下载完成后的UI
        TextView finish = new TextView(getActivity());
        finish.setText("all downloaded");

        // 网络异常的UI
        TextView unreachNet = new TextView(getActivity());
        unreachNet.setText("unreach Network");

        // 网络数据异常的UI
        TextView error = new TextView(getActivity());
        error.setText("error");

        // 加载中..  的UI
        ProgressBar loading = new ProgressBar(getActivity());
        loading.setIndeterminate(true);
        BottomLoadStateView layout = new BottomLoadStateView(getActivity(), idle, loading, error, finish, unreachNet);
        return layout;
    }


    @Override
    public View createHeaderView(LayoutInflater inflater) {
        TextView tvHeaderView = new TextView(getActivity());
        tvHeaderView.setText("我是HeaderView");
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tvHeaderView.setLayoutParams(lp);
        tvHeaderView.setGravity(Gravity.CENTER);
        return tvHeaderView;
    }



    /**
     * Adapter
     */
    public class TemplateListAdapter extends RecyclerViewBaseAdapter<TemplateBean> {

        public TemplateListAdapter(Context con) {
            super(con);
        }

        @Override
        public RecyclerView.ViewHolder onCreateHolder(ViewGroup viewGroup, int i) {
            return new ItemViewHolder(setAdapterViewItem(viewGroup, R.layout.listitem_template));
        }


        @Override
        public void onBindHolder(RecyclerView.ViewHolder viewHolder, int i) {
            ItemViewHolder holder = (ItemViewHolder) viewHolder;
            final TemplateBean bean = getData().get(i);
//            ImageLoader.with(RequestDemoListActivity.this).load(bean.getIconUrl()).into(holder.ivIcon).start();
            holder.tvText.setText(bean.getText());
        }

        public class ItemViewHolder extends RecyclerView.ViewHolder {
            public RelativeLayout lytMain;
            public TextView tvText;
            public ImageView ivIcon;

            public ItemViewHolder(View itemView) {
                super(itemView);
                lytMain = (RelativeLayout) itemView.findViewById(R.id.lytMain);
                ivIcon = (ImageView) itemView.findViewById(R.id.ivIcon);
                tvText = (TextView) itemView.findViewById(R.id.tvText);
            }
        }
    }


    /**
     * 数据对象 Bean
     */
    public static class TemplateBean {
        @SerializedName("icoUrl")
        String icoUrl;

        String name;

        public String getIconUrl() {
            return icoUrl;
        }

        public void setIconUrl(String iconUrl) {
            this.icoUrl = iconUrl;
        }

        public String getText() {
            return name;
        }

        public void setText(String text) {
            this.name = text;
        }
    }
}
